import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 页面tab栏数据
const pageTabState = {
  namespaced: true,
  state: () => ({
    pageTabs: [],
    currentPageTitle: '',
    currentPageIndex: ''
  }),
  getters: {
    getPageTabs: state => {
      return state.pageTabs
    }
  },
  mutations: {
    sliceTab(state, Payload) {
      let { i, position } = Payload
      let arr = null
      if (position === 'left') {
        arr = state.pageTabs.slice(i)
        let firstTab = state.pageTabs.slice(0, 1)
        arr = [...firstTab, ...arr]
      } else if (position === 'right') {
        arr = state.pageTabs.slice(0, i)
      }
      state.pageTabs = arr
    },
    deleteTab(state, Payload) {
      state.pageTabs.splice(Payload, 1)
    },
    deleteAllTab(state) {
      state.pageTabs.splice(1)
    },
    deleteOtherTab(state, Payload) {
      let arr = []
      state.pageTabs.forEach((el, i) => {
        if (i === 0 || el.title === Payload) {
          arr.push(el)
        }
      })
      state.pageTabs = arr
    },
    setPageTabs(state, Payload) {
      let pushItem = true
      state.pageTabs.forEach((el, i) => {
        if (el.title === Payload.title) {
          pushItem = false
          state.pageTabs[i] = Payload
        }
      })
      pushItem && state.pageTabs.push(Payload)
    },
    setFirstTab(state, Payload) {
      let unshiftItem = true
      state.pageTabs.forEach((el, i) => {
        if (el.title === Payload.title) {
          unshiftItem = false
          state.pageTabs[i] = Payload
        }
      })
      unshiftItem && state.pageTabs.unshift(Payload)
    },
    setCurrentPageTitle(state, Payload) {
      state.currentPageTitle = Payload
    },
    setCurrentPageIndex(state, Payload) {
      state.currentPageIndex = Payload
    }
  },
  actions: {}
}
// 权限管理
const authority = {
  namespaced: true,
  state: () => ({ routeTree: {} }),
  getters: {},
  mutations: {
    setRouteTree(state, Payload) {
      if (Payload?.children?.length) {
        state.routeTree = Payload
      } else {
        state.routeTree = {}
      }
    }
  },
  actions: {}
}
// loading 状态
const XLoading = {
  namespaced: true,
  state: () => ({ loading: false, loadingText: '加载中...' }),
  getters: {},
  mutations: {
    setLoadingState(state, Payload) {
      state.loading = Payload.loading
      state.loadingText = Payload.loadingText
    }
  },
  actions: {}
}
// 馆藏书业务清单
const bookBusinessData = {
  namespaced: true,
  state: () => ({
    untreated_number: 0,
    undeliver_number: 0,
    other_order_number: 0
  }),
  getters: {},
  mutations: {
    bookBusinessState(state, Payload) {
      state.untreated_number = Payload.untreated_number
      state.undeliver_number = Payload.undeliver_number
      state.other_order_number = Payload.other_order_number
    }
  },
  actions: {}
}
// 书店业务清单
const shopBusinessData = {
  namespaced: true,
  state: () => ({
    untreated_number: 0,
    undeliver_number: 0,
    other_order_number: 0
  }),
  getters: {},
  mutations: {
    shopBusinessState(state, Payload) {
      state.untreated_number = Payload.untreated_number
      state.undeliver_number = Payload.undeliver_number
      state.other_order_number = Payload.other_order_number
    }
  },
  actions: {}
}

const store = new Vuex.Store({
  modules: {
    XLoading,
    pageTabState,
    authority,
    bookBusinessData,
    shopBusinessData
  }
})

export default store
